//
//  BK_My_DetailOrder_SectionModel.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/2/17.
//  Copyright © 2017年 utouu. All rights reserved.
//

//
//  BKMyOrderDetailSectionModel.m
//  bestkeep
//
//  Created by Mr. Wu on 16/12/12.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BK_My_DetailOrder_SectionModel.h"
#import "BK_My_DetailOrder_CellModel.h"
#import "BKMyOrderDetailCell.h"
#import "BKMyOrderDerTableviewCellHederAndFooter.h"
#import "BK_ODetail_Tool.h"
#define WIDTH_LEFT   34  //左边间距
#define WIDTH_TOP    15  //上边间距
#define WIDTH_DOWN   15  //下边间距
@implementation BK_My_DetailOrder_SectionModel
+(NSArray *)CreateOrderSectionWithArray:(BK_My_DetailOrder_Model*)model{
    BK_My_DetailOrder_SectionModel *section = [[BK_My_DetailOrder_SectionModel alloc]init];
    
    
    //交易关闭;section0;
    NSMutableArray *SectionArry = [NSMutableArray array];
    
    BK_My_DetailOrder_CellModel *OrderstatusCell = [[BK_My_DetailOrder_CellModel  alloc]init];
    OrderstatusCell.reuseIdentifier = @"OrderstatusCell";
    OrderstatusCell.OrderModel =model;
    OrderstatusCell.height = 55;
    OrderstatusCell.className = NSStringFromClass([BK_OD_Status class]);
    BK_My_DetailOrder_SectionModel *OrderstatusSection = [[BK_My_DetailOrder_SectionModel alloc]init];
    OrderstatusSection.headerhHeight = 10;
    OrderstatusSection.footerHeight = 10;
    OrderstatusSection.cells = @[OrderstatusCell];
    OrderstatusSection.Ordermodel = model;
    [SectionArry addObject:OrderstatusSection];
    
    
    //收货地址; section1;
    
    BK_My_DetailOrder_CellModel *DetailAddressCell = [[BK_My_DetailOrder_CellModel  alloc]init];
    DetailAddressCell.reuseIdentifier = @"DetailAddressCell";
    //高提前算出来;
    DetailAddressCell.height = [section getHeightWithModel:model ];
    DetailAddressCell.className = NSStringFromClass([BK_OD_Address class]);
    DetailAddressCell.OrderModel = model;
    
    BK_My_DetailOrder_SectionModel *DetailAddressSection = [[BK_My_DetailOrder_SectionModel alloc]init];
    DetailAddressSection.headerhHeight = 0;
    DetailAddressSection.footerHeight =10;
    DetailAddressSection.cells = @[DetailAddressCell];
    DetailAddressSection.Ordermodel = model;
    [SectionArry addObject:DetailAddressSection];
    
    //商品;section2;
    NSMutableArray *array = [NSMutableArray array];
    for (BK_My_DetailOrder_OrderList_Model *listdel in model.order_list) {
        BK_My_DetailOrder_CellModel *OrderGoods = [[BK_My_DetailOrder_CellModel  alloc]init];
        OrderGoods.className = NSStringFromClass([BK_OD_Goods class]);
        OrderGoods.OrderlistModel = listdel;//子商品
        
        /*
         * 手动赋值
         */
        OrderGoods.OrderlistModel.thNormalType=model.thNormalType;
        
        
        OrderGoods.OrderModel  = model;//订单;
        OrderGoods.reuseIdentifier = @"OrderGoods";
        OrderGoods.height = 75+10+5;
        NSInteger count = model.order_list.count;
        if (listdel == model.order_list[count-1]) {
            OrderGoods.height = 75+10;
        }
        [array addObject:OrderGoods];
    }
    
    BK_My_DetailOrder_SectionModel *Ordersection = [[BK_My_DetailOrder_SectionModel alloc]init];
    Ordersection.HeadeViewClassName = NSStringFromClass([BKMyOrderTableviewCellHeder class]);
    Ordersection.reuseIdentifier = @"Ordersection";
    Ordersection.headerhHeight = 32;
//    if (model.lastSendDateZh&&[model.order_status isEqualToString:@"02"]&&[model.thNormalType isEqualToString:@"1"]) {
//        Ordersection.footerHeight = 10;
//    }else{
        Ordersection.footerHeight = 0.00001;
//    }
//    Ordersection.footerHeight = 10;
//    Ordersection.footerHeight = 0.00001;
    Ordersection.cells = array;
    Ordersection.Ordermodel = model;
    [SectionArry addObject:Ordersection];
    
    
    //预计发货日期;
    //BKOrderExpectedSendTimeCell
    
    if (model.lastSendDateZh&&[model.order_status isEqualToString:@"02"]&&[model.thNormalType isEqualToString:@"1"]) {
        
        BK_My_DetailOrder_CellModel *sendtime = [[BK_My_DetailOrder_CellModel alloc]init];
        sendtime.className = NSStringFromClass([BK_OD_ExpectedSendTime class]);
        sendtime.reuseIdentifier = @"sendTime";
        sendtime.OrderModel = model;
        sendtime.height = 35;
        
        BK_My_DetailOrder_SectionModel *OrderSendTime = [[BK_My_DetailOrder_SectionModel alloc]init];
        
        OrderSendTime.cells = @[sendtime];
        OrderSendTime.headerhHeight = 0;
        OrderSendTime.headerhHeight = 0.01;
        OrderSendTime.footerHeight = 10;
        
        OrderSendTime.Ordermodel = model;
        [SectionArry addObject:OrderSendTime];
    }
    
    
    
    
    //金额,税费,运费 section3;
    BK_My_DetailOrder_CellModel *OrderMoney = [[BK_My_DetailOrder_CellModel alloc]init];
    OrderMoney.className = NSStringFromClass([BK_OD_MoneyDetail class]);
    OrderMoney.reuseIdentifier = @"ordermoney";
    OrderMoney.OrderModel = model;
    CGFloat Hight = [section getOrderMoneyHeight:model];
    OrderMoney.height = Hight;
    BK_My_DetailOrder_SectionModel *OrderMoneySection = [[BK_My_DetailOrder_SectionModel alloc]init];
    OrderMoneySection.FootViewClassName = NSStringFromClass([BKMyOrderDerTableviewCellFooter class]);
    OrderMoneySection.reuseIdentifier = @"OrderMoneySection";
    OrderMoneySection.cells = @[OrderMoney];
    OrderMoneySection.headerhHeight = 0;
    OrderMoneySection.footerHeight= [section getFootHeight:model];
    OrderMoneySection.Ordermodel = model;
    [SectionArry addObject:OrderMoneySection];
    
    
    //下单时间,订单号section4;
    BK_My_DetailOrder_CellModel *OrderTime = [[BK_My_DetailOrder_CellModel alloc]init];
    OrderTime.className = NSStringFromClass([BK_OD_TimeList class]);
    OrderTime.height = [section getOrderTimeSectionHeight:model];
    OrderTime.reuseIdentifier = @"OrderTime";
    OrderTime.OrderModel = model;
    
    BK_My_DetailOrder_SectionModel *OrderTimeSection = [[BK_My_DetailOrder_SectionModel alloc]init];
    OrderTimeSection.cells = @[OrderTime];
    OrderTimeSection.headerhHeight = 10;
    OrderTimeSection.footerHeight = 10;
    OrderTimeSection.Ordermodel  = model;
    [SectionArry addObject:OrderTimeSection];
    return SectionArry;
}




-(CGFloat )getHeightWithModel:(BK_My_DetailOrder_Model*)centerModel
{
    if (centerModel)
    {
        NSString * str_address=[NSString stringWithFormat:@"收货地址：%@",centerModel.deliver_address];
        CGFloat  width_address= [self getWidthWithTitle:str_address font:14.0];
        CGFloat  width_address1=ScreenWidth-WIDTH_LEFT-WIDTH_RIGHT;
        if (width_address>width_address1)
        {
            return WIDTH_TOP+WIDTH_TOP+5+20+40;
        }
        else
        {
            return WIDTH_TOP+WIDTH_TOP+5+20+20;
        }
    }
    else
    {
        return 100;
    }
}

/**
 *   获取字符宽度
 */
- (CGFloat)getWidthWithTitle:(NSString *)title font:(CGFloat )font
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
    label.text = title;
    label.font = [UIFont systemFontOfSize:font];
    [label sizeToFit];
    return label.frame.size.width;
}
-(float)getOrderMoneyHeight:(BK_My_DetailOrder_Model*)model{
    int i = 2;
    i += ![model.is_global isEqualToString:@"0"]?1:0;
    i += ![model.promotion_discount_amount isEqualToString:@"0.00"]?1:0;
    i += ![model.shoppingTicketAmount isEqualToString:@"0.00"]?1:0;
    return i*26+20;
}
-(float)getOrderTimeSectionHeight:(BK_My_DetailOrder_Model*)model{
    
#define  HEIGHT_BKOrderListCell  73
    /*
     订单状态：
     01，待支付                     去付款
     02，已支付（待发货）    取消订单
     03，已发货                     查看物流 ，收货确认
     04，交易成功                  删除订单，查看物流，去评价
     05，交易关闭
     06，已取消
     07，已退款
     */
    if ([ model.order_status isEqualToString:@"01"])
    {
        return HEIGHT_BKOrderListCell-20;
    }
    else if([ model.order_status isEqualToString:@"05"])
    {
        return HEIGHT_BKOrderListCell-20;
    }
    
    return HEIGHT_BKOrderListCell;
    
}
-(float)getFootHeight:(BK_My_DetailOrder_Model*)model{
    
    NSString *stutus = model.order_status;
    //已支付,已发货,交易成功显示实际付款;
    if ([stutus isEqualToString:@"02"]||[stutus isEqualToString:@"03"]||[stutus isEqualToString:@"04"]) {
        return 44+70;
        
    }
    //否则,不显示;
    else{
        return 44+45;
    }
    
}

@end
